﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Actuals.CoreDomain;
using Actuals.Data;
using Actuals.CoreDomain.Data;

namespace Actuals.Data
{
    public class DalUserStory :Dal<IUserStory,UserStory>,IDalUserStory
    {
        private readonly IDalManager _manager;
        private static readonly IDalUserStory _instance = new DalUserStory();
        private const string  SP_GET_USERSTORIES = "usp_GetUserStories";
        private const string SP_GET_USERSTORY = "usp_GetUserStory";
        private const string SP_GET_OLDUSERSTORY = "usp_GetOldUserStory";
        private const string SP_ADD_USERSTORIES = "usp_Add_UserStories";
        private const string SP_DELETE_USERSTORY = "usp_Delete_UserStory";
        private const string SP_UPDATE_USERSTORY = "usp_UpdateUserStory";
        private const string SP_GET_USERSTORIES_WITH_SPRINT = "usp_GetUserStoriesWithSprint";
      //  private const string SP_ADD_USERSTORY_AND_RETURN_USERSTORYID = "usp_AddUserStoryAndReturnUserStoryId";

        public static IDalUserStory Instance
        {
            get { return _instance; }
        }
        public DalUserStory()
        {
            _manager = new DalManager();
        }

        public DalUserStory(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IUserStory obj, DataRow row)
        {
            obj.TaskId = _manager.GetValue<int>(row, "TaskId");
            obj.UserStoryId = _manager.GetValue<int>(row, "USerStoryId");
            obj.OldUserStoryId = _manager.GetValue<int>(row, "OldUSerStoryId");
            obj.UserStoryDescription = _manager.GetValue<string>(row, "UserStoryDescription");
            obj.Sprint = _manager.GetValue<string>(row, "Sprint");
            obj.StoryPoints = _manager.GetValue<int>(row, "StoryPoints");
        }

     /*   public IList<IUserStory> GetUserStories()
        {
                _manager.StoredProcedureName = SP_GET_USERSTORIES;
                List<IUserStory> userStories = new List<IUserStory>();
                foreach(IUserStory userStory in ConvertAll(_manager.ExecuteDataSet()))
                    userStories.Add(userStory);
                return userStories;

        }*/

        public IUserStory GetUserStory(int userStoryId)
        {
            _manager.StoredProcedureName = SP_GET_USERSTORY;
            _manager.AddInParameter("@UserStoryId",DbType.Int32,userStoryId);
            IUserStory userStory = Convert(_manager.ExecuteDataSet());
            return userStory;
             
        }

        public IUserStory GetOldUserStory(int oldUserStoryId)
        {
            _manager.StoredProcedureName = SP_GET_OLDUSERSTORY;
            _manager.AddInParameter("@OldUserStoryId", DbType.Int32, oldUserStoryId);
            IUserStory userStory = Convert(_manager.ExecuteDataSet());
            return userStory;

        }

        public bool AddUserStory(IUserStory userStory)
        {
            _manager.StoredProcedureName = SP_ADD_USERSTORIES;
             _manager.AddInParameter("@UserStoryId",DbType.Int32,userStory.UserStoryId);
             _manager.AddInParameter("@OldUserStoryId", DbType.Int32, userStory.OldUserStoryId);
             _manager.AddInParameter("@TaskId", DbType.Int32, userStory.TaskId);
            _manager.AddInParameter("@UserStoryDescription",DbType.String, userStory.UserStoryDescription);
            _manager.AddInParameter("@Sprint", DbType.Int32, userStory.Sprint);
            _manager.AddInParameter("@StoryPoints", DbType.Int32, userStory.StoryPoints);
            _manager.ExecuteNonQuery();
            return true;

        }

      /*  public bool AddUserStoryAndReturnUserStoryId(IUserStory userStory)
        {
            int userStoryId;
            _manager.StoredProcedureName = SP_ADD_USERSTORY_AND_RETURN_USERSTORYID;
             _manager.AddInParameter("@TaskId", DbType.Int32, userStory.TaskId);
            _manager.AddInParameter("@UserStoryDescription",DbType.String, userStory.UserStoryDescription);
            _manager.AddInParameter("@Sprint", DbType.Int32, userStory.Sprint);
            _manager.AddInParameter("@StoryPoints", DbType.Int32, userStory.StoryPoints);
            _manager.AddInParameter("@UserStoryId", DbType.Int32, ParameterDirection.Output);
            _manager.ExecuteNonQuery();
            userStoryId = int.Parse(_manager.GetParameterValue("@UserStoryId").ToString());

            
            return true;
        }*/
        public bool DeleteUserStory(int userStoryId)
        {
            _manager.StoredProcedureName = SP_DELETE_USERSTORY;
            _manager.AddInParameter("@UserStoryId", DbType.Int32, userStoryId);
            _manager.ExecuteNonQuery();
            return true;
                            
        }
        public IList<IUserStory> GetUserStories(int taskId)
        {
            _manager.StoredProcedureName = SP_GET_USERSTORIES;
            _manager.AddInParameter("@TaskId", DbType.Int32, taskId);
            IList<IUserStory> userStories = new List<IUserStory>();
            foreach (IUserStory userStory in ConvertAll(_manager.ExecuteDataSet()))
                userStories.Add(userStory);
            return userStories;

            
        }
        public  int UpdateUserStory(IUserStory userStory)
        {
            _manager.StoredProcedureName = SP_UPDATE_USERSTORY;
           
            _manager.AddInParameter("@UserStoryId", DbType.Int32, userStory.UserStoryId);
            _manager.AddInParameter("@TaskId", DbType.Int32, userStory.TaskId);
            _manager.AddInParameter("@UserStoryDescription", DbType.String, userStory.UserStoryDescription);
            _manager.AddInParameter("@Sprint", DbType.Int32, userStory.Sprint);
            _manager.AddInParameter("@StoryPoints", DbType.Int32, userStory.StoryPoints);
            return _manager.ExecuteNonQuery();
           
        }
        public IList <IUserStory> GetUserStoriesWithSprint(int sprint)
        {
            _manager.StoredProcedureName = SP_GET_USERSTORIES_WITH_SPRINT;
            _manager.AddInParameter("@Sprint", DbType.Int32, sprint);
          IUserStory userStory = Convert(_manager.ExecuteDataSet());
            IList<IUserStory> userStories = new List<IUserStory>();
            foreach (IUserStory userStory1 in ConvertAll(_manager.ExecuteDataSet()))
                userStories.Add(userStory1);
            return userStories;

           
        }


       



    

    



    }
}

        
     